package com.fruit.account.service.usercount;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fruit.mapper.account.commission.CommissionDetailMapper;
import com.fruit.pojo.account.usercount.UserCommissionCount;
import com.fruit.sdk.account.usercount.UserCountService;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户邀请统计相关Service
 * 
 * @author Administrator
 * @version 2017.11.30 11:40
 */
@Slf4j
@Service("userCountService")
public class UserCountServiceImpl implements UserCountService {
	@Autowired
	private CommissionDetailMapper commissionDetailMapper;

	@Override
	public Map<String, Object> countUserCommissions(Map<String, Object> params) {
		log.info("【用户下级贡献佣金统计】---【开始】---");
		Map<String, Object> result = new HashMap<String, Object>();
		if (params == null) {
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数不能为null!"));
			return result;
		}
		log.info("【用户下级贡献佣金统计】---【params】---{}", params.toString());
		if (StringUtils.isEmpty(String.valueOf(params.get("userId")))) {
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数用户id不能为空!"));
			return result;
		}
		try {
			// 统计用户下级贡献佣金明细
			List<UserCommissionCount> list = commissionDetailMapper.countUserCommissions(params);
			// 统计用户下级贡献佣金总金额
			Integer userId = Integer.valueOf(String.valueOf(params.get("userId")));
			String money = commissionDetailMapper.getTotalMoneyByUserId(userId);

			result.put("dataList", list);
			result.put("money", money);
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			log.error("【用户下级贡献佣金统计】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【用户下级贡献佣金统计】---【结束】---");
		return result;
	}

}
